package com.zdzx.controller.admin.content;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdzx.base.common.util.RedisUtil;
import com.zdzx.base.common.util.UserInfoUtil;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.CourseMapper;
import com.zdzx.mapper.DicMapper;
import com.zdzx.model.service.base.result.UserInfo;
import com.zdzx.model.service.base.vo.Dic;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "统计")
@RestController
@RequestMapping("/admin")
public class StatisticsController {

	@Autowired
	private CourseMapper courseMapper;
	@Autowired
	private DicMapper dicMapper;
	@Autowired
	private RedisUtil redisUtil;
	
	@ApiOperation(value = "", notes = "")
	@GetMapping("/statistics/course/type")
	public Result<List<Map<String,String>>> courseType(
			@ApiParam("") @RequestParam(value = "tenantId", required = false) String tenantId,
			HttpServletRequest request){
		UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("`type`", "course_type");
		List<Dic> selectList = dicMapper.selectList(queryWrapper);
		List<Map<String,String>> list= courseMapper.courseBrowseStatistics(tenantId);
		List<Map<String,String>> result=new ArrayList<Map<String,String>>();
		for (Dic dic : selectList) {
			Map<String,String> map=new HashMap<String, String>();
			map.put("code", dic.getValue());
			map.put("count", "0");
			for (Map<String, String> map2 : list) {
				if(map2.containsKey(dic.getCode())) {
					map.put("count", map2.get("count"));
				}
			}
			result.add(map);
		}
		
		
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, result);
	}
	
	
	@ApiOperation(value = "", notes = "")
	@GetMapping("/statistics/course/category")
	public Result<List<Map<String,String>>> courseType1(
			@ApiParam("") @RequestParam(value = "tenantId", required = false) String tenantId,
			HttpServletRequest request){
		UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		List<Map<String,String>> list=courseMapper.courseBrowseStatistics1(tenantId);
		
		QueryWrapper<Dic> queryWrapper=new QueryWrapper<Dic>();
		queryWrapper.eq("`type`", "course_category");
		List<Dic> selectList = dicMapper.selectList(queryWrapper);
		List<Map<String,String>> result=new ArrayList<Map<String,String>>();
		for (Dic dic : selectList) {
			Map<String,String> map=new HashMap<String, String>();
			map.put("code", dic.getValue());
			map.put("count", "0");
			for (Map<String, String> map2 : list) {
				if(map2.containsKey(dic.getCode())) {
					map.put("count", map2.get("count"));
				}
			}
			result.add(map);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, result);
	}
	

}
